<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>收入统计</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>




<table id="incomeT" lay-filter="incomeT"></table>
<script src="/layui/layui.js"></script>
<script>
layui.config({
    base: '/webui/'  // 模块目录
}).extend({
    "cosjs":"{/}/layui/?name=cosjs",
}).use(['form','table','cosjs','element'], function () {
    let form = layui.form,cosjs=layui.cosjs,$ = layui.jquery;
    let table=layui.table;

    let _ret=<%-JSON.stringify(ret)%>;

    let _head=[];
    let _hh=[];
    let _data=[];
    _hh.push({field: 'time', title: '日期', width:100, fixed: 'left'});
    for(let s of _ret.server){
      _hh.push({field: `s${s.sid}`, title: s.sname, width:120, fixed: 'left'});
    }
     _hh.push({field: 'total', title: '汇总('+_ret.setting.mName+')', width:160, fixed: 'left'});
    if(_ret.auth&&_ret.auth.inner){
      _hh.push({field: 'rmb', title: `折合（${_ret.setting.rate}）/RMB`, width:200, fixed: 'left'});
      _hh.push({field: 'frmb', title: `最终分成（${_ret.setting.offset}）/RMB`, width:200, fixed: 'left'});
    }
    _head.push(_hh);

    // _head.push(_hh);

    // _head.push(_hh);

    let head=999999999;
    let tail=0;
    for(let s of _ret.server){
      for(let j of s.coll){
        head=Math.min(head,j.time);
        tail=Math.max(tail,j.time);
      }
    }

 


    for(let i=head;i<=tail;++i){
      let _row={'time':i};
      let _total=0;
      for(let s of _ret.server){
          _row[`s${s.sid}`]=0;
          for(let j of s.coll){
            if(j.time==i){
              _row[`s${s.sid}`]=j.income;
              _total+=j.income;
            }
          }
      }
      _row['total']=_total;
      if(_total>0){
        if(_ret.auth&&_ret.auth.inner){
          _row['rmb']=(_total*_ret.setting.rate).toFixed(0);
          _row['frmb']=(_total*_ret.setting.rate*_ret.setting.offset).toFixed(0);
        }
        _data.push(_row);
      }
    }


 

    _data.sort((x,y)=>{return y.time-x.time});



 









        //table.data=_coll;

        table.render({
        elem: '#incomeT'
        ,height: 960
        ,limit:30
        // ,url: '/demo/table/user/' //数据接口
        ,page: true //开启分页
        ,cols:_head,
        data:_data
      });


      // }).catch(err=>{

      //   cosjs.alert(err);
      // })    








    // form.on('submit(roleOK)', function(data){
    //   //向服务器查询
    //   let index=window.location.pathname.indexOf('/income');
    //   let head=window.location.pathname.slice(0,index);
    //   let  url =`${head}/income/query`;
    //   cosjs.post(url,data.field).then(ret=>{


    //     //table.data=_coll;

    //     table.render({
    //     elem: '#incomeT'
    //     ,height: 960
    //     ,limit:30
    //     // ,url: '/demo/table/user/' //数据接口
    //     ,page: true //开启分页
    //     ,cols: [[ //表头
    //       {field: 'time', title: '日期', width:200, fixed: 'left'}
    //       ,{field: 'income', title: '收入', width:200,sort: true,}
    //     ]],
    //     data:ret.ret,
    //   });


    //   }).catch(err=>{

    //     cosjs.alert(err);
    //   })    
    //   return false;
    // })


  function formatDate(date) {
    var date = new Date(date);
    var YY = date.getFullYear() + '-';
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
    var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return YY + MM + DD +" "+hh + mm + ss;
  }


});
</script>
</body>
</html>